<?php
namespace app\api\model;
use think\Config;
use think\Db;
class IndexModel extends \app\common\model\CommonModel
{
    /**
     * @param string    $method
     * @param int       $catid
     */
    public function list($params)
    {
        try {
            switch ($params['method']) {
                case 'cat':
                    //分类
                    $res = Db::table('cat')->order('id desc')->select();
                    break;
                case 'tool':
                    //工具
                    $field = "a.id,a.catid,b.cat,a.icon,a.name,a.path,a.status,a.visible,a.`desc`,a.tags,a.sort";
                    $sql = "SELECT {$field} FROM tool a LEFT JOIN cat b ON a.catid = b.id WHERE a.visible = 1 ";
                    if(!empty($params['catid'])){
                        $sql .= "AND `catid` = {$params['catid']} ";
                    }
                    if(!empty($params['exact'])){
                        $sql .= "AND ( a.name LIKE '%{$params['exact']}%' OR a.tags LIKE '%{$params['exact']}%' OR b.cat LIKE '%{$params['exact']}%' OR a.`desc` LIKE '%{$params['exact']}%' ) ";    
                    }
                    $order = "ORDER BY sort DESC";
                    $sql .= $order;
                    $res = $this->getRes($sql); 
                    break;
                default:
                    throw new \Exception("参数错误"); 
                    break;
            }
            return $res;    
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }    
    }
}